############################################################
## Fraga, Juenke, and Shah 2019: One Run Leads to Another?
## Replication Code: January 2, 2018
## Figure A2: Candidate Emergence, African-Americans by % Latino and Latinos by % Black
############################################################

# Required Libraries
require(ggplot2)

# Load Data #
sl_data <- read.csv("FragaJuenkeShah_JOP_Data.csv", stringsAsFactors=FALSE)

# Remove all districts where 1+ candidates have no race data
sl_data <- subset(sl_data, unknown_cand == 0)

# Change candidate variable to a binary, 0 = no candidate, 1 = one or more candidates
sl_data$white_cand[sl_data$white_cand > 1] <- 1
sl_data$black_cand[sl_data$black_cand > 1] <- 1
sl_data$latino_cand[sl_data$latino_cand > 1] <- 1
sl_data$asian_cand[sl_data$asian_cand > 1] <- 1

## FigA2a, Left Panel of Figure A2: African-Americans
figA2a <- ggplot(data=sl_data, aes(x=latino_pct, y=black_cand))
figA2a <- figA2a + geom_segment(aes(y=0.5,yend=0.5,x=-Inf,xend=Inf), size=0.5, linetype=2, lineend="round", color="gray70")
figA2a <- figA2a + stat_smooth(method = "loess", size=1.5, se=FALSE)
figA2a <- figA2a + geom_point(alpha=0.2)
figA2a <- figA2a + ggtitle("African-Americans, by Percent Latino")
figA2a <- figA2a + scale_x_continuous("Percent Latino in State Legislative District")
figA2a <- figA2a + scale_y_continuous("Probability of Black Candidate", limits=c(-0.05,1.05))
figA2a <- figA2a + coord_cartesian(ylim=c(0,1))
figA2a <- figA2a + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA2a

## FigA2b, Right Panel of Figure A2: Latinos
figA2b <- ggplot(data=sl_data, aes(x=black_pct, y=latino_cand))
figA2b <- figA2b + geom_segment(aes(y=0.5,yend=0.5,x=-Inf,xend=Inf), size=0.5, linetype=2, lineend="round", color="gray70")
figA2b <- figA2b + stat_smooth(method = "loess", size=1.5, se=FALSE)
figA2b <- figA2b + geom_point(alpha=0.2)
figA2b <- figA2b + ggtitle("Latinos, by Percent Black")
figA2b <- figA2b + scale_x_continuous("Percent Black in State Legislative District")
figA2b <- figA2b + scale_y_continuous("Probability of Latino Candidate", limits=c(-0.05,1.05))
figA2b <- figA2b + coord_cartesian(ylim=c(0,1))
figA2b <- figA2b + theme_bw() + theme(
	panel.grid.minor=element_blank(), 
	panel.grid.major=element_blank(),
	legend.title=element_blank())
figA2b